<?php
namespace Home\Controller;
use Think\Controller;

  
 
class AccountsafeController extends Controller {


	//判断是否已设置密保（如果已设置，需验证密保）
	public function index(){
		$UserId = I('session.menuid');
		$data = M('tbuserinformation')->field('cpwdq1,cpwdq2')->where("nuserid=$UserId")->select();
		$q1 = $data[0]['cpwdq1'];
		$q2 = $data[0]['cpwdq2'];
			
		if(!empty($q1) && !empty($q2) ){
			$this->assign('q1',$q1);
			$this->assign('q2',$q2);
			$this->display('questionVerfy');
		}else{
			$this->display('checkpsw');
		}			
	}

	public function checkpw(){
		$UserId = I('session.menuid');
		$psw = I('post.opsw');
		$psw = CreatePassword($psw);		
		$user = M('tbuserinformation')->field('cpassword')->where(array(
        'nuserid' => $UserId,
		))->find(); 	


		if($psw==$user['cpassword']){
			echo 1;
		}else {
			echo 2;
		}
	}
	
//修改密码数据处理
	public function resetpswCheck(){
	
		if(IS_POST){
		$UserId = I('session.menuid');
		$psw = I('post.opsw'); 
		$npsw = I('post.npsw');
		$newpsw['cpassword'] = CreatePassword($npsw);		
		$user = M('tbuserinformation')->field('nuserid,cpassword,cname')->where(array(
        'nuserid' => $UserId,
		))->find(); 
		
		
		  if (empty($psw)) {
			  echo 1;
		  }
		  elseif (!CheckPassword($psw , $user['cpassword'])) {
			echo 2;
		  }elseif(empty($npsw)){
		   echo 7;
		  }
		  elseif(strlen($npsw)<6){
			echo 3;
		  }

		  elseif(I('post.npsw_sure')!=$npsw){
			echo 5;
		  } elseif(M('tbuserinformation')->where("nuserid=$UserId")->save($newpsw)){
		    echo 6;
		   };		
		
		}	
	
	}
	
	//修改密码
	public function resetpsw(){

			$this->display(); 

	
	}
	
	//密保问题数据处理.1为密保验证数据处理。2为密保更新数据处理
	public function setquestionCheck(){
	if($_POST){
		$UserId = I('session.menuid');
		$data = M('tbuserinformation')->field('cpwda1,cpwda2')->where("nuserid=$UserId")->select();			
		if(I('post.from')==1){
			
		$cpwda1 =CreatePassword(I('post.ans1'));//密保问题1答案
		$cpwda2 =CreatePassword(I('post.ans2'));	//密保问题2答案			
		if(empty($cpwda1)){
			echo 1; //问题1为空
		}else if(empty($cpwda2)){
			echo 2;  //问题2为空
		} elseif($cpwda1!=$data[0]['cpwda1']){
		    echo 3; //问题1答案错误
		}else if($cpwda2!=$data[0]['cpwda2']){
			echo 4;//问题2答案错误
		}else if($cpwda1==$data[0]['cpwda1'] && $cpwda2==$data[0]['cpwda2']){
		    echo 5;
		   }	
		}else if(I('post.from')==2){
		$qustions = array(
		
		'1'=>"母亲的姓名",
		'2'=>"爷爷的姓名",
		'3'=>"我的小学校名",
		'4'=>"我的中学校名",
		'5'=>"我最喜欢的歌曲",
		'6'=>"我最喜欢的运动",
		'7'=>"我个人计算机的型号",
		'8'=>"驾驶执照最后八位数字",
		
		);

		$cpwdq1 = I('post.ques1'); //密保问题1

		$cpwda1 =I('post.ans1');//密保问题1答案
		$cpwdq2 = I('post.ques2');  //密保问题2
	
		$cpwda2 =I('post.ans2');	//密保问题2答案	

		if(!is_numeric($cpwdq1)){
			$cpw['cpwdq1'] =$cpwdq1;
		}else{
			$cpw['cpwdq1'] =$qustions[$cpwdq1];
		}
		if(!is_numeric($cpwdq2)){
			$cpw['cpwdq2'] =$cpwdq2;
		}else{
			$cpw['cpwdq2'] =$qustions[$cpwdq2];
		}		
		$cpw['cpwda1'] =CreatePassword($cpwda1);
		$cpw['cpwda2'] =CreatePassword($cpwda2);	
		
		if(empty($cpwdq1)){
			echo 1;   
		}else if(empty($cpwda1)){
			echo 2;
		}else if(empty($cpwdq2)){
			echo 3;
		}else if(empty($cpwda2)){
			echo 4;
		} else{
			M('tbuserinformation')->where("nuserid=$UserId")->save($cpw);
		if(empty($data[0]['cpwda1']) && empty($data[0]['cpwda2'])){			
			 echo 5;
		}else{		
			 echo 6;
		}
		   
		   }
	
		}	
	}
	
	
	}
	
	//密保问题验证
	public function questionVerfy(){

		$this->display();
	}
	
	

	
	//密保问题提交
	public function question(){
		$this->display();	
	}
	
	//登陆记录
	public function loginRecord(){
	
		$UserId = I('session.menuid');
		$count =M('tbuserlogin')->where("nuserid=$UserId")->count();
		$items =13;
		$Page = new \Think\Userpage($count,$items);
			
		$data = M('tbuserlogin')->field('dlogin,cip,clocation')
					  ->where("nuserid=$UserId")
					  ->limit($Page->firstRow.','.$Page->listRows)
					  ->order('dlogin desc')
					  ->select();
		$listshow = $Page->show(); 
		$this->assign('list',$listshow);
		$this->assign('data',$data);
		$this->display();		
	
	}
	
}